/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.dnd.rtftransfer;


private import dwt.dwt;

private import dwt.dnd.bytearraytransfer;
private import dwt.dnd.transferdata;
private import dwt.internal.ole.win32.com;
private import dwt.internal.ole.win32.OBJIDL;


/**
 * The class <code>RTFTransfer</code> provides a platform specific mechanism 
 * for converting text in RTF format represented as a java <code>String</code> 
 * to a platform specific representation of the data and vice versa.  See 
 * <code>Transfer</code> for additional information.
 * 
 * <p>An example of a java <code>String</code> containing RTF text is shown 
 * below:</p>
 * 
 * <code><pre>
 *     String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}";
 * </code></pre>
 */
public class RTFTransfer : ByteArrayTransfer {

	private import std.c.windows.windows;
	
	private static RTFTransfer _instance;
	private static char[] CF_RTF = "Rich Text Format"; //$NON-NLS-1$
	private static int CF_RTFID;
	private static int CodePage;

	private static boolean staticCtor_done = false;
	
private this() {
	if(!staticCtor_done) {
		staticCtor_done = true;
		_instance = new RTFTransfer();
		CF_RTFID = registerType(CF_RTF);
		CodePage = COM.GetACP();
	}
}

/**
 * Returns the singleton instance of the RTFTransfer class.
 *
 * @return the singleton instance of the RTFTransfer class
 */
public static RTFTransfer getInstance () {
	return _instance;
}

/**
 * This implementation of <code>javaToNative</code> converts RTF-formatted text
 * represented by a java <code>String</code> to a platform specific representation.
 * For additional information see <code>Transfer#javaToNative</code>.
 * 
 * @param object a java <code>String</code> containing RTF text
 * @param transferData an empty <code>TransferData</code> object; this
 *  object will be filled in on return with the platform specific format of the data
 */
public void javaToNative (Object object, TransferData transferData){
	transferData.result = COM.E_FAIL;
	if (object is null ) return;

	StringObj arbObj = cast(StringObj)object;
	if(arbObj is null || arbObj.data is null) return;
 
	if (!isSupportedType(transferData)) {
		// did not match the TYMED
		transferData.stgmedium = new STGMEDIUM();
		transferData.result = COM.DV_E_TYMED;
		return;
	}
	// CF_RTF is stored as a null terminated byte array
	char[] string = arbObj.data;
	int count = string.length;
	if (count == 0) return;
	wchar[] chars = new wchar[count + 1];
	
	chars[0..count] = Utf.toString16(string)[0..count];
	
	int cchMultiByte = COM.WideCharToMultiByte(CodePage, 0, chars.ptr, -1, null, 0, null, null);
	if (cchMultiByte == 0) {
		transferData.stgmedium = new STGMEDIUM();
		transferData.result = COM.DV_E_STGMEDIUM;
		return;
	}
	HGLOBAL lpMultiByteStr = COM.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte);
	COM.WideCharToMultiByte(CodePage, 0, chars.ptr, -1, cast(char*)lpMultiByteStr, cchMultiByte, null, null);
	transferData.stgmedium = new STGMEDIUM();
	transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
	transferData.stgmedium.unionField = lpMultiByteStr;
	transferData.stgmedium.pUnkForRelease = null;
	transferData.result = COM.S_OK;
	return;
}

/**
 * This implementation of <code>nativeToJava</code> converts a platform specific 
 * representation of RTF text to a java <code>String</code>.
 * For additional information see <code>Transfer#nativeToJava</code>.
 * 
 * @param transferData the platform specific representation of the data to be 
 * been converted
 * @return a java <code>String</code> containing RTF text if the 
 * conversion was successful; otherwise null
 */
public Object nativeToJava(TransferData transferData){
	if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null;
	IDataObject data = transferData.pIDataObject;
	data.AddRef();
	STGMEDIUM* stgmedium = new STGMEDIUM();
	FORMATETC* formatetc = transferData.formatetc;
	stgmedium.tymed = COM.TYMED_HGLOBAL;	
	transferData.result = data.GetData(formatetc, stgmedium);
	data.Release();	
	if (transferData.result != COM.S_OK) return null;
	HGLOBAL hMem = cast(HGLOBAL)stgmedium.unionField;
	try {
		HGLOBAL lpMultiByteStr = cast(HGLOBAL)COM.GlobalLock(hMem);
		if (lpMultiByteStr is null) return null;
		try {
			int cchWideChar  = COM.MultiByteToWideChar (CodePage, COM.MB_PRECOMPOSED, cast(char*)lpMultiByteStr, -1, null, 0);
			if (cchWideChar == 0) return null;
			wchar[] lpWideCharStr = new wchar [cchWideChar - 1];
			COM.MultiByteToWideChar (CodePage, COM.MB_PRECOMPOSED, cast(char*)lpMultiByteStr, -1, lpWideCharStr.ptr, lpWideCharStr.length);
			StringObj obj = new StringObj;
			obj.data = Utf.toString16(lpWideCharStr);			
			return obj;
		} finally {
			COM.GlobalUnlock(hMem);
		}
	} finally {
		COM.GlobalFree(hMem);
	}
	// return null;
}
protected int[] getTypeIds(){
	int[] intArr;
	intArr ~= CF_RTFID;
	return intArr;
}
protected char[][] getTypeNames(){
	char[][] StringArr;
	StringArr ~= (CF_RTF);
	return StringArr; 
}

boolean checkRTF(Object object) {
	return (object !is null  && cast(StringObj)object && (cast(StringObj)object).data.length > 0);
}

boolean validate(Object object) {
	return checkRTF(object);
}

}
